home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1728 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  57 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A Very Simple Socket Question
  5. Date: 16 Jan 1996 16:16:06 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan16111606@g7240065.bridge.bst.bls.com>
  8. References: <4dfgj8$6p9@nntp.ucs.ubc.ca>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: evil@unixg.ubc.ca's message of 16 Jan 1996 06:28:56 GMT
  11.  
  12. In article <4dfgj8$6p9@nntp.ucs.ubc.ca> evil@unixg.ubc.ca (Peter Pan) writes:
  13.  
  14. : Please help! I am a C beginner.
  15.  
  16. : ======================================
  17. : Client:
  18.  
  19. : int sd, addrlen;
  20. : struct sockaddr_in svaddr;
  21.  
  22. : char *data;
  23. : data = "abcdef";
  24.  
  25. : .... /* skip */
  26.  
  27. : sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
  28.  
  29.  
  30. : ==========================================
  31. : Server:
  32.  
  33. : char data;
  34.  
  35. I assume this was meant to say:
  36.   char* data;
  37.  
  38. : data = (char*) malloc( 100*sizeof(char) );
  39.  
  40. : recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
  41.                       ^^^^^^^^^^^^
  42. Your problem lies here. 'data' is a pointer and the sizeof a pointer,
  43. obviously on your machine, is 4 bytes.
  44. Replace this line with:
  45.  
  46.   recvfrom(sd2, data, 100 * sizeof(char), 0, &claddr, &addrlen);
  47.  
  48. Though the multiplication of sizeof(char) is unnecessary as by definition
  49. this is 1.
  50.  
  51. Hope this helps
  52.  
  53.    -A.
  54.  
  55. -- 
  56. | A.Champion                |
  57.